package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.basicInformation.label.OneLabelDto;
import com.dg.cuckoo.admin.dto.basicInformation.label.TwoLabelDto;
import com.dg.cuckoo.admin.dto.basicInformation.projectInformation.BasicProjectDto;
import com.dg.cuckoo.admin.dto.basicInformation.projectInformation.TheLabelDto;
import com.dg.cuckoo.admin.repository.*;
import com.dg.cuckoo.admin.service.ProjectInfoService;
import com.dg.cuckoo.common.utils.ExcelUtil;
import com.dg.cuckoo.common.utils.ListUtil;
import com.dg.cuckoo.core.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ProjectInfoServiceimpl implements ProjectInfoService {

    @Autowired
    private OneLabelRepository oneLabelRepository;
    @Autowired
    private TwoLableRepository twoLableRepository;
    @Autowired
    private BasicProjectRepository basicProjectRepository;
    @Autowired
    private TheLabelRepository theLabelRepository;
    @Autowired
    private TheUnitRepository theUnitRepository;


    @Override
    public List<String> openOneLabel() {
        List<OneLabelDto> byHeadLabel = oneLabelRepository.findByHeadLabel(1);
        List<String> data = new ArrayList<>();
        for (OneLabelDto o : byHeadLabel) {
            data.add(o.getLabelName());
        }
        return data;
    }

    @Override
    public List<String> openTwoLabel(String oneLabel) {
        List<String> data = new ArrayList<>();
        if (oneLabel == null) {
            return data;
        }
        List<TwoLabelDto> dtos = twoLableRepository.findTwoLabelDtosByOneLabel(oneLabel);
        for (TwoLabelDto t : dtos) {
            data.add(t.getLabelName());
        }
        return data;
    }

    @Override
    public BasicProjectDto openInfo(String id) {
        if (id == null) {
            throw new ServiceException("请选择行");
        }
        return basicProjectRepository.findById(id).get();
    }

    @Override
    public List<BasicProjectDto> query(String oneLabel, String twoLabel, String unit, String year, String mainName) {
        List<String> ids = findIds(oneLabel, twoLabel, unit, year, mainName);
        return basicProjectRepository.findAllById(ids);
    }

    @Override
    public void inport(List<BasicProjectDto> list) throws IOException {
        String uuid = UUID.randomUUID().randomUUID().toString();
        String path = "E:\\java\\2020\\eleventh_month\\back-stage_management\\dg\\cuckoo\\src\\main\\webapp\\excel\\" + uuid + ".xlsx";
        ExcelUtil.write(list, path);
    }

    @Override
    public List<String> findIds(String oneLabel, String twoLabel, String unit, String year, String mainName) {
        List<BasicProjectDto> byLable = new ArrayList<>();
        List<BasicProjectDto> byLable1 = new ArrayList<>();
        List<BasicProjectDto> byCodingTime = new ArrayList<>();
        List<BasicProjectDto> byMainNameLike = new ArrayList<>();
        if (oneLabel != null) {
            List<TheLabelDto> theLabelDtos = theLabelRepository.findByLable(oneLabel);
            for (TheLabelDto t : theLabelDtos) {
                byLable.add(basicProjectRepository.findById(t.getByMark()).get());
            }
        }
        if (twoLabel != null) {
            List<TheLabelDto> theLabelDtos = theLabelRepository.findByLable(twoLabel);
            for (TheLabelDto t : theLabelDtos) {
                byLable1.add(basicProjectRepository.findById(t.getByMark()).get());
            }
        }
        if (year != null) {
            byCodingTime = basicProjectRepository.findByCodingTime(year);
        }
        if (mainName != null) {
            byMainNameLike = basicProjectRepository.findByMainNameContaining(mainName);
        }
        int util = util2(oneLabel, twoLabel, unit, year, mainName);
        if(util>1){
            //获取交集list
            List<BasicProjectDto> list = ListUtil.receiveCollectionList(ListUtil.receiveCollectionList(byLable, byLable1), ListUtil.receiveCollectionList(byCodingTime, byMainNameLike));
            List<String> ids = new ArrayList<>();
            for (BasicProjectDto b : list) {
                ids.add(b.getId());
            }
            return ids;
        }
        byLable.addAll(byLable1);
        byLable.addAll(byCodingTime);
        byLable.addAll(byMainNameLike);
        List<String> ids = new ArrayList<>();
        for (BasicProjectDto b : byLable) {
            ids.add(b.getId());
        }
        List<String> result=ids.stream().distinct().collect(Collectors.toList());
        return result;
    }

    @Override
    public List<String> findIdsByList(List<String> oneLabel, List<String> twoLabel, String unit, String year, String mainName) {
        List<BasicProjectDto> byLable = new ArrayList<>();
        List<BasicProjectDto> byLable1 = new ArrayList<>();
        List<BasicProjectDto> byCodingTime = new ArrayList<>();
        List<BasicProjectDto> byMainNameLike = new ArrayList<>();
        if (oneLabel != null) {
            List<TheLabelDto> theLabelDtos = theLabelRepository.findByLableIn(oneLabel);
            for (TheLabelDto t : theLabelDtos) {
                byLable.add(basicProjectRepository.findById(t.getByMark()).get());
            }
        }
        if (twoLabel != null) {
            List<TheLabelDto> theLabelDtos = theLabelRepository.findByLableIn(twoLabel);
            for (TheLabelDto t : theLabelDtos) {
                byLable1.add(basicProjectRepository.findById(t.getByMark()).get());
            }
        }
        if (year != null) {
            byCodingTime = basicProjectRepository.findByCodingTime(year);
        }
        if (mainName != null) {
            byMainNameLike = basicProjectRepository.findByMainNameContaining(mainName);
        }
        int util = util(oneLabel, twoLabel, unit, year, mainName);
        if (util > 1) {
            List<BasicProjectDto> dtos = ListUtil.receiveCollectionList(byLable, byLable1);
            //获取交集list
            List<BasicProjectDto> list = ListUtil.receiveCollectionList(ListUtil.receiveCollectionList(byLable, byLable1), ListUtil.receiveCollectionList(byCodingTime, byMainNameLike));
            List<String> ids = new ArrayList<>();
            for (BasicProjectDto b : list) {
                ids.add(b.getId());
            }
            return ids;
        }
        byLable.addAll(byLable1);
        byLable.addAll(byCodingTime);
        byLable.addAll(byMainNameLike);
        List<String> ids = new ArrayList<>();
        for (BasicProjectDto b : byLable) {
            ids.add(b.getId());
        }
        List<String> result=ids.stream().distinct().collect(Collectors.toList());
        return result;
    }

    public int util(List<String> oneLabel, List<String> twoLabel, String unit, String year, String mainName) {
        int result = 0;
        if (oneLabel != null&& oneLabel.size()!=0) {
            result++;
        }
        if (twoLabel != null&& twoLabel.size()!=0) {
            result++;
        }
        if (unit != null&& unit.equals("")) {
            result++;
        }
        if (year != null&& year.equals("")) {
            result++;
        }
        if (mainName != null&& mainName.equals("")) {
            result++;
        }
        return result;
    }

    public int util2(String oneLabel, String twoLabel, String unit, String year, String mainName) {
        int result = 0;
        if (oneLabel != null&& oneLabel.length()!=0) {
            result++;
        }
        if (twoLabel != null&& twoLabel.length()!=0) {
            result++;
        }
        if (unit != null&& unit.equals("")) {
            result++;
        }
        if (year != null&& year.equals("")) {
            result++;
        }
        if (mainName != null&& mainName.equals("")) {
            result++;
        }
        return result;
    }
}
